/
ElementWrapper`1.cs
191 lines (174 loc) · 7.13 KB
/
ElementWrapper`1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
namespace Legerity;
using System;
using Legerity.Exceptions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Support.UI;
/// <summary>
/// Defines a base wrapper for elements to expose platform element logic.
/// </summary>
/// <typeparam name="TElement">
/// The type of <see cref="AppiumWebElement"/>.
/// </typeparam>
public abstract class ElementWrapper<TElement> : IElementWrapper<TElement>
where TElement : AppiumWebElement
{
private readonly WeakReference elementReference;
/// <summary>
/// Initializes a new instance of the <see cref="ElementWrapper{TElement}"/> class.
/// </summary>
/// <param name="element">
/// The <typeparamref name="TElement"/> reference.
/// </param>
protected ElementWrapper(TElement element)
{
this.elementReference = new WeakReference(element);
}
/// <summary>Gets the original <typeparamref name="TElement"/> reference object.</summary>
public TElement Element =>
this.elementReference is { IsAlive: true }
? this.elementReference.Target as TElement
: null;
/// <summary>
/// Gets the driver used to find this element.
/// </summary>
public IWebDriver ElementDriver => this.Element.WrappedDriver;
/// <summary>
/// Gets a value indicating whether the element is visible.
/// </summary>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
public virtual bool IsVisible => this.Element.Displayed;
/// <summary>
/// Gets a value indicating whether the element is enabled.
/// </summary>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
public virtual bool IsEnabled => this.Element.Enabled;
/// <summary>
/// Clicks the element.
/// </summary>
/// <exception cref="InvalidElementStateException">Thrown when an element is not enabled.</exception>
/// <exception cref="ElementNotVisibleException">Thrown when an element is not visible.</exception>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
public virtual void Click()
{
this.Element.Click();
}
/// <summary>
/// Gets the value of the specified attribute for this element.
/// </summary>
/// <param name="attributeName">The name of the attribute.</param>
/// <returns>The attribute's current value if it exists; otherwise, null.</returns>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
public string GetAttribute(string attributeName)
{
return this.Element.GetAttribute(attributeName);
}
/// <summary>
/// Finds a child element by the specified locator.
/// </summary>
/// <param name="locator">The locator to find a child element by.</param>
/// <returns>The <typeparamref name="TElement"/>.</returns>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public AppiumWebElement FindElement(By locator)
{
return this.Element.FindElement(locator);
}
/// <summary>
/// Determines whether the given element is not shown.
/// </summary>
/// <param name="locator">
/// The locator for the element to locate.
/// </param>
/// <exception cref="ElementShownException">Thrown when the element is shown for the expected locator.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public void VerifyElementNotShown(By locator)
{
try
{
this.VerifyElementShown(locator);
throw new ElementShownException(locator.ToString());
}
catch (ElementNotShownException)
{
}
}
/// <summary>
/// Determines whether the given element is shown.
/// </summary>
/// <param name="locator">
/// The locator for the element to find.
/// </param>
/// <exception cref="ElementNotShownException">Thrown when an element is not shown for the expected locator.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public void VerifyElementShown(By locator)
{
this.VerifyElementShown(locator, null);
}
/// <summary>
/// Determines whether the specified element is shown with the specified timeout.
/// </summary>
/// <param name="locator">The locator to find a specific element.</param>
/// <param name="timeout">
/// The amount of time the driver should wait when searching for the <paramref name="locator"/> if it is not immediately present.
/// </param>
/// <exception cref="ElementNotShownException">Thrown when an element is not shown for the expected locator.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public void VerifyElementShown(By locator, TimeSpan? timeout)
{
if (timeout == null)
{
try
{
if (this.Element.FindElement(locator) == null)
{
throw new ElementNotShownException(locator.ToString());
}
}
catch (NoSuchElementException ex)
{
throw new ElementNotShownException(locator.ToString(), ex);
}
}
else
{
var wait = new WebDriverWait(this.Element.WrappedDriver, timeout.Value);
wait.Until(driver => driver.FindElement(locator) != null);
}
}
/// <summary>
/// Determines whether the specified elements are shown.
/// </summary>
/// <param name="locator">
/// The locator for the element to find.
/// </param>
/// <exception cref="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
public void VerifyElementsShown(By locator)
{
this.VerifyElementsShown(locator, null);
}
/// <summary>
/// Determines whether the specified elements are shown with the specified timeout.
/// </summary>
/// <param name="locator">
/// The locator to find a collection of elements.
/// </param>
/// <param name="timeout">
/// The amount of time the driver should wait when searching for the <paramref name="locator"/> if it is not immediately present.
/// </param>
/// <exception cref="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
public void VerifyElementsShown(By locator, TimeSpan? timeout)
{
if (timeout == null)
{
if (this.Element.FindElements(locator).Count == 0)
{
throw new ElementsNotShownException(locator.ToString());
}
}
else
{
var wait = new WebDriverWait(this.Element.WrappedDriver, timeout.Value);
wait.Until(driver => driver.FindElements(locator).Count != 0);
}
}
}